package com.yinhe.music.yhmusic.login.validcode;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.login.password.PasswordActivity;
import com.yinhe.music.yhmusic.model.ValidCodeInfo;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hasee on 2017/3/20.
 */

public class CheckCodeActivity extends BaseServiceActivity implements ValidCodeContract.IValidCodeView {
    private TextView time, recent;
    Timer timer = new Timer();
    private int type;
    private int ttl = 60;
    private Button finish;
    private EditText edt_code;
    private String code;
    private String phone;

    ValidCodePresenter mPresenter;

    @Override
    public void initView() {
        RxBus.get().register(this);

        if (getIntent() != null) {
            type = getIntent().getIntExtra("type", 1);
            phone = getIntent().getStringExtra("phone");
        }

        time = findViewById(R.id.time);
        recent = findViewById(R.id.recent);
        finish = findViewById(R.id.finish);
        edt_code = findViewById(R.id.code);
        finish.setText("下一步");

        edt_code.setFocusable(true);
        edt_code.requestFocus();
        edt_code.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        setRegister();
        setToolbar();
        timer.schedule(task, 1000, 1000);
        recent.setOnClickListener(view -> resendCode());
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_register2;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new ValidCodePresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(() -> {
                ttl--;
                time.setText(ttl + "s");
                if (ttl < 0) {
                    timer.cancel();
                    time.setVisibility(View.GONE);
                    recent.setVisibility(View.VISIBLE);
                } else {
                    recent.setVisibility(View.GONE);
                }
            });
        }
    };

    private void setRecentTask() {
        timer = new Timer();
        TimerTask recentTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(() -> {
                    ttl--;
                    time.setText(ttl + "s");
                    if (ttl < 0) {
                        timer.cancel();
                        time.setVisibility(View.GONE);
                        recent.setVisibility(View.VISIBLE);
                    } else {
                        recent.setVisibility(View.GONE);
                        time.setVisibility(View.VISIBLE);
                    }
                });
            }
        };
        timer.schedule(recentTask, 1000, 1000);
    }

    private void setRegister() {
        finish.setOnClickListener(view -> {
            code = edt_code.getText().toString().trim();
            if (TextUtils.isEmpty(code)) {
                Toast.makeText(CheckCodeActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            checkCode();
        });
    }

    private void checkCode() {
        mPresenter.checkValidCode(phone, code);
    }

    private void setToolbar() {
        switch (type) {
            case 0:
                setToolbar("重设密码");
                break;
            case 1:
                setToolbar("注册");
                break;
        }
    }

    private void resendCode() {
        mPresenter.getValidCode(phone);
        ttl = 60;
    }

    @Override
    public void startNext(ValidCodeInfo info) {
        Toast.makeText(CheckCodeActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(CheckCodeActivity.this, PasswordActivity.class);
        intent.putExtra("code", info.getValidCode());
        intent.putExtra("phone", info.getPhone());
        intent.putExtra("type", type);
        startActivity(intent);
    }

    @Override
    public void getCodeUI() {
        setRecentTask();
    }
}
